package com.ease.util
{

	import mx.controls.CheckBox;
	import mx.controls.ComboBox;
	import mx.controls.DateField;
	import mx.controls.RadioButton;
	import mx.controls.RadioButtonGroup;
	import mx.controls.TextArea;
	import mx.controls.TextInput;
	import mx.core.Container;
	import mx.core.IVisualElementContainer;

	import spark.components.BorderContainer;
	import spark.components.CheckBox;
	import spark.components.ComboBox;
	import spark.components.DropDownList;
	import spark.components.RadioButton;
	import spark.components.RadioButtonGroup;
	import spark.components.TextArea;
	import spark.components.TextInput;

	public class FormUtil
	{
		private static var _instance:FormUtil=new FormUtil();

		public function FormUtil()
		{
		}

		public static function getInstance():FormUtil
		{
			return _instance;
		}

		public function wrapperFormData(container:Object, bo:Object):void
		{
			if (bo == null)
			{
				throw new Error("收集表单数据的对象不能为空!");
			}
			var count:Number=container.numChildren;
			for (var i:Number=0; i < count; i++)
			{
				var obj:Object=container.getChildAt(i);
				if (isTextField(obj))
				{
					appendProperty(bo, obj, obj.text);
				}
				else if (isCheckBox(obj))
				{
					appendProperty(bo, obj, obj.selected ? "1" : "0");
				}
				else if (isRadioButton(obj))
				{
					if (obj.groupName != null)
					{
						bo[obj.groupName]=obj.group.selectedValue;
						continue;
					}
					if (obj.selected)
					{
						appendProperty(bo, obj, obj.value);
					}
				}
				else if (isComboBox(obj))
				{
					var result:Object=(obj.selectedItem == null || obj.selectedItem is String || !obj.selectedItem.hasOwnProperty("data")) ? null : obj.selectedItem.data;
					appendProperty(bo, obj, result);
				}
				else if (isDropDownList(obj))
				{
					var dr:DropDownList=obj as DropDownList;
					appendProperty(bo, obj, dr.selectedItem.data);
				}
				else if (isRadioButtonGroup(obj))
				{
					//取不到  
				}
				else if (obj is Container || obj is IVisualElementContainer)
				{
					wrapperFormData(obj, bo);
				}
			}
		}

		/***
		 * 为对象增加指定属性和属性值

		 */
		private function appendProperty(bo:Object, obj:Object, propertyValue:Object):void
		{
			if (!obj.hasOwnProperty("id"))
			{
				return;
			}
			bo[obj["id"]]=propertyValue;
		}

		/***
		 * 重置form中的项

		 */
		public function resetForm(container:Object):void
		{
			var count:Number=container.numChildren;
			for (var i:Number=0; i < count; i++)
			{
				var obj:Object=container.getChildAt(i);
				if (isTextField(obj))
				{
					obj.text="";
				}
				else if (isCheckBox(obj))
				{
					obj.selected=false;
				}
				else if (isRadioButton(obj))
				{
					if (obj.groupName != null)
					{
						obj.group.selectedValue=null;
						continue;
					}
					if (obj.groupName == undefined || obj.groupName == "")
					{
						obj.selected=false;
					}
				}
				else if (isComboBox(obj))
				{
					obj.selectedIndex=0;
				}
				else if (isRadioButtonGroup(obj))
				{
					obj.selectedValue=obj.getRadioButtonAt(1).value;
				}
				else if (isDropDownList(obj))
				{
					obj.selectedIndex=0;
				}
				else if (obj is Container || obj is IVisualElementContainer)
				{
					resetForm(obj);
				}
			}
		}

		/***
		 * 根据对象填充表单
		 */
		public function fillForm(rec:Object, container:Object):void
		{
			if (rec == null)
			{
				throw new Error("指定的数据源为空！");
			}
			var count:Number=container.numChildren;
			for (var i:Number=0; i < count; i++)
			{
				var obj:Object=container.getChildAt(i);
				var name:String=getObjectName(rec, obj);
				if (isFormControl(obj))
				{
					if (name == null)
					{
						continue;
					}
				}
				var value:String=getValue(rec, name);
				if (isTextField(obj))
				{
					obj.text=value;
				}
				else if (isCheckBox(obj))
				{
					if (value == "1")
					{
						obj.selected=true;
					}
				}
				else if (isRadioButton(obj))
				{
					if (obj.groupName != null)
					{
						obj.group.selectedValue=value;
					}
					else
					{
						if (obj.data == value)
						{
							obj.selected=true;
						}
					}
				}
				else if (isComboBox(obj) || isDropDownList(obj))
				{
					var source:Object=obj.dataProvider;
					var sel:Object=getItemByData(source, value);
					if (sel == null)
					{
						obj.selectedIndex=0;
					}
					else
					{
						obj.selectedItem=sel;
					}
				}
				else if (obj is Container || obj is IVisualElementContainer)
				{
					fillForm(rec, obj);
				}
			}
		}

		private function getValue(rec:Object, name:String):String
		{
			return name == null ? "" : rec[name];
		}

		private function getObjectName(rec:Object, obj:Object):String
		{
			var name:String=obj.hasOwnProperty("id") ? obj.id : null;
			if (isRadioButton(obj) && obj && obj.groupName != null)
			{
				name=obj.groupName;
			}
			if (rec.hasOwnProperty(name))
			{
				return name;
			}
			return null;
		}

		private function isFormControl(obj:Object):Boolean
		{
			if (isTextField(obj))
			{
				return true;
			}
			if (isCheckBox(obj) || isRadioButton(obj) || isComboBox(obj))
			{
				return true;
			}
			return false;
		}

		/**
		 * source Combox下拉列表数据源
		 * itemData 下拉数据源中某项的data属性值
		 * return itemData对应的item
		 */
		public function getItemByData(source:Object, itemData:Object):Object
		{
			if (source == null || itemData == null)
			{
				return null;
			}
			for each (var obj:Object in source)
			{
				if (obj.data == itemData)
				{
					return obj;
				}
			}
			return null;
		}

		private function isDropDownList(obj:Object):Boolean
		{
			return obj is DropDownList;
		}

		private function isRadioButtonGroup(obj:Object):Boolean
		{
			return obj is mx.controls.RadioButtonGroup || obj is spark.components.RadioButtonGroup;
		}

		private function isRadioButton(obj:Object):Boolean
		{
			return obj is mx.controls.RadioButton || obj is spark.components.RadioButton;
		}

		private function isComboBox(obj:Object):Boolean
		{
			return obj is mx.controls.ComboBox || obj is spark.components.ComboBox;
		}

		private function isCheckBox(obj:Object):Boolean
		{
			return obj is mx.controls.CheckBox || obj is spark.components.CheckBox;
		}

		private function isTextField(obj:Object):Boolean
		{
			if (obj is mx.controls.TextInput || obj is mx.controls.TextArea || obj is mx.controls.DateField || obj is spark.components.TextInput || obj is spark.components.TextArea)
			{
				return true;
			}
			return false;
		}
	}
}